<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<t:base type="jquery,easyui,tools,DatePicker,bootstrap"></t:base>
<link rel="stylesheet" href="plug-in/ztree/css/zTreeStyle.css"
	type="text/css">
<script charset="utf-8" src="plug-in/ztree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript"
	src="plug-in/ztree/js/jquery.ztree.core-3.5.min.js"></script>
<script type="text/javascript" src="plug-in/ztree/js/ztreeCreator.js"></script>
<script type="text/javascript"
	src="plug-in/ztree/js/jquery.ztree.excheck-3.5.min.js"></script>
<div class="" fit="" scroll="" style="width: 100%;float:left;">
	<div style="width:400px;height:450px;overflow: auto;float:left;">
		<t:tabs id="tabsOne" iframe="false" tabPosition="top" fit="false">
			<t:tab href="" icon="icon-search" title="用户" id="tab1"></t:tab>
			<t:tab href="" icon="icon-search" title="角色" id="tab2"></t:tab>
			<t:tab href="" icon="icon-search" title="机构" id="tab3"></t:tab>
		</t:tabs>
		<div>
			<span>名称</span> <input id="search" type="text" value=""> <a
				href="#" class="easyui-linkbutton" iconCls="icon-search"
				onclick="btnClick();">查询</a>
		</div>
		<div style="background-color: #5c9ccc;width: 60px;">
			<span style="color: #fff">授权对象</span>
		</div>
		<div id="orgTree" class="ztree"
			style="border: 1px solid #4297d7;height: 300px;overflow:auto"></div>
	</div>
	<div style="width:400px;height:450px;overflow: auto;float:left;">
		<div style="background-color: #5c9ccc;width: 135px;">
			<span style="color: #fff">授权类型：授权功能</span>
		</div>
		<tr>
			<td align="right">
				<label class="Validform_label"> 应用系统:</label>
			</td>
			<td class="value"><t:dictSelect id="appid" field="appid"
					type="list" dictTable="SYS_APP" dictField="appid"
					dictText="appname" hasLabel="false" title="系统"></t:dictSelect></td>
			<td><input id="saveBtn" type="button" value="保存授权"
				style="width: 80px;height: 35px;border: 1px solid #ccc;"></td>
		</tr>
		<div style="height: 380px;width:300px;overflow:auto">
			<ul id="rigTree" class="ztree"></ul>
		</div>
	</div>
</div>
<script>
	$(function() {
		loadTree();//左布局数据
		rightTree();//右布局数据
	});
</script>
<script src="plug-in/hplus/plugins/suggest/bootstrap-suggest.min.js"></script>
<script>
var checkIndex=1;
var datalist=0;
var leftList=[];
var treeNodeId="";
var authType=1;
//左布局tab选项卡
var getData="getUser";
var tabsUrl="sysMenuController.do?";
var Url=tabsUrl+getData+"&searchText=";

$(function(){
	$("#tabsOne").click(function() {
		var tabName=$("#tabsOne").context.activeElement.innerText;
		if (tabName=="用户") {
			authType=1;
			getData="getUser";
			Url=tabsUrl+getData+"&searchText=";
			loadTree()
			$("#search").val("");
		}else if(tabName=="角色"){
			authType=2;
			getData="getRole";
			Url=tabsUrl+getData+"&searchText=";
			loadTree()
			$("#search").val("");
		}else{
			authType=3;
			getData="getOrgnization";
			Url=tabsUrl+getData+"&searchText=";
			loadTree()
			$("#search").val("");
		}
	});
})

//搜索框
var searchText;
function btnClick() {
	searchText=$("#search").val();
	Url=tabsUrl+getData+"&searchText="+searchText;	
	loadTree();
}

//左布局获取数据
var orgTree ;
function loadTree() {
	var zNodes;
	jQuery.ajax({  
        async : false,  
        cache:false,  
        type: 'POST',  
        dataType : "json",  
        url: Url,//请求的action路径  
        error: function () {//请求失败处理函数  
            alert('请求失败');  
        },  
        success:function(data){ //请求成功后处理函数。
            zNodes = data.obj;   //把后台封装好的简单Json格式赋给zNodes
            //Url="";
            //$("#search").val("");
        }  
    });
	var ztreeCreator = new ZtreeCreator('orgTree',"sysMenuController.do?",zNodes)
 			.setCallback({onClick:zTreeOnLeftClick})
 			.initZtree({},function(treeObj){orgTree = treeObj})
};


//左布局左击关联右布局对应数据
function zTreeOnLeftClick(event, treeId, treeNode) {
	treeNodeId=treeNode.id;
	//获取用户或角色或机构对应的权限id
	$("#appid ").get(0).selectedIndex=1; 
	leftList.splice(0,leftList.length);
		jQuery.ajax({  
	        async : false,  
	        cache:false,  
	        type: 'POST',  
	        dataType : "json",  
	        url: "sysMenuController.do?userToPower&userId="+treeNode.id,//请求的action路径  
	        error: function () {//请求失败处理函数  
	            alert('请求失败');  
	        },  
	        success:function(data){ //请求成功后处理函数。
	            datalist = data.obj;   //把后台封装好的简单Json格式赋给zNodes 
	        }  
	    });
	    for ( var i = 0; i < datalist.length; i++) {
			leftList[i]=datalist[i].id;
		}
	   //把权限id传后台得到对应的权限数据
		var zNodesList;
		jQuery.ajax({  
	        async : false,  
	        cache:false,  
	        type: 'POST',  
	        dataType : "json",  
	        url: "sysMenuController.do?getTreeDemoData&appId=0&datas="+leftList.join(),//请求的action路径  
	        error: function () {//请求失败处理函数  
	            alert('请求失败');  
	        },  
	        success:function(data){ //请求成功后处理函数。
	            zNodesList = data.obj;   //把后台封装好的简单Json格式赋给zNodes  
	        }  
	    });
	   	var ztreeCreator = new ZtreeCreator('rigTree',"sysMenuController.do?getTreeDemoData&appId=0&datas="+leftList.join(),zNodesList)
	 			.setCallback({onClick:rigZTreeOnLeftClick})
	 			.setCheckboxType({ "Y": "ps", "N": "ps" })
	 			.initZtree({},function(treeObj){rigTree = treeObj})
};
//右布局下拉框
var selectUrl="sysMenuController.do?getTreeDemoData&appId=1&datas=0";
$(function () {
	 $("#appid ").get(0).selectedIndex=1; 
	 $("#appid").change(function(){
	 		selectUrl="";
	 		 checkIndex=$("#appid ").get(0).selectedIndex;
	 		 if (checkIndex!=0) {
	 		 	console.log(leftList.join()+"---"+checkIndex);
				selectUrl="sysMenuController.do?getTreeDemoData&appId="+checkIndex+"&datas="+leftList.join();
			 	rightTree();
			 } 
	 });  
})
$(function() {
	$("#saveBtn").click(function() {
		fun_getCheckValue();
	})
})


function SelectNode(id) {
         var treeObj = $.fn.zTree.getZTreeObj("rigTree");
          
         var treenode = treeObj.getNodeByParam("id",id , null);
         treeObj.expandNode(treenode, true, true, true);
         treeObj.selectNode(treenode);
     }
        
function  fun_getCheckValue(){
   var zTree = $.fn.zTree.getZTreeObj("rigTree");
			var nodes=zTree.getChangeCheckedNodes(true);
				var result='';
			 for (var i = 0; i < nodes.length; i++) {
				var halfCheck = nodes[i].getCheckStatus();
				 if (!halfCheck.half){
					result += nodes[i].id +',';
				 }
			}
			result=result.substring(0,result.lastIndexOf(","));
			console.log(result)
			jQuery.ajax({  
		        async : false,  
		        cache:false,  
		        type: 'POST',  
		        dataType : "json",  
		        url: "sysMenuController.do?savePower&appId="+checkIndex+"&privilegeId="+result+"&memberid="+treeNodeId+"&authType="+authType+"",//请求的action路径  
		        error: function () {//请求失败处理函数  
		            alert('请求失败');  
		        },  
		        success:function(data){ //请求成功后处理函数。
		            alert("保存成功！");
		        }  
		    }); 
}
	
//右布局获取数据
var rigTree ;
function rightTree() {
	var zNodes;
	jQuery.ajax({  
        async : false,  
        cache:false,  
        type: 'POST',  
        dataType : "json",  
        url: selectUrl,//请求的action路径  
        error: function () {//请求失败处理函数  
            alert('请求失败');  
        },  
        success:function(data){ //请求成功后处理函数。
            zNodes = data.obj;   //把后台封装好的简单Json格式赋给zNodes  
            //console.log(data.obj)
        }  
    });
	var ztreeCreator = new ZtreeCreator('rigTree',"sysMenuController.do?getTreeDemoData",zNodes)
 			.setCallback({onClick:rigZTreeOnLeftClick})
 			.setCheckboxType({ "Y": "ps", "N": "ps" })
 			.initZtree({},function(treeObj){rigTree = treeObj})
};
//右布局左击
function rigZTreeOnLeftClick(event, treeId, treeNode) {
	SelectNode(treeNode.id)
};
</script>
<script type="text/javascript">
//保存授权点击事件

</script>
